<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/common/commonPage.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加内容</title>
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="${resourcesUrl}/common/css/smartadmin-production.min.css">
</head>
<body>
    <%@ include file="/WEB-INF/page/common/header.jsp"%>
    <%@ include file="/WEB-INF/page/common/leftSideBar.jsp"%>

    <div id="main" role="main">
        <div id="ribbon">
            <ol class="breadcrumb">
                <li>首页</li>
                <li>内容管理</li>
                <li>添加内容</li>
            </ol>
        </div>
        <div id="content">
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="jarviswidget">
                        <header>
                            <h2><i class="fa fa-plus"></i> 添加内容</h2>
                        </header>
                        <div class="widget-body">
                            <form id="addForm" class="smart-form" enctype="multipart/form-data">
                                <input type="hidden" name="columnId" value="${columnId}">
                                <fieldset>
                                    <section>
                                        <label class="label">内容标识</label>
                                        <label class="input">
                                            <i class="icon-append fa fa-pencil"></i>
                                            <input type="text" name="contentMark" class="form-control">
                                        </label>
                                    </section>
                                    <section>
                                        <label class="label">标题</label>
                                        <label class="input">
                                            <i class="icon-append fa fa-header"></i>
                                            <input type="text" name="title" class="form-control">
                                        </label>
                                    </section>
                                    <section>
                                        <label class="label">简要描述</label>
                                        <label class="textarea">
                                            <i class="icon-append fa fa-pencil"></i>
                                            <textarea name="description" rows="3" class="form-control"></textarea>
                                        </label>
                                    </section>
                                    <section>
                                        <label class="label">缩略图</label>
                                        <div class="input-group">
                                            <label class="input">
                                                <i class="icon-append fa fa-picture-o"></i>
                                                <input type="text" name="pic" id="picPath" class="form-control" readonly>
                                            </label>
                                            <label for="picFile" class="input-group-btn">
                                                <span class="btn btn-primary">
                                                    <i class="fa fa-folder-open"></i> 选择图片
                                                    <input type="file" name="picFile" id="picFile" style="display:none" accept=".jpg,.jpeg,.png,.gif">
                                                </span>
                                            </label>
                                        </div>
                                        <div id="picPreview" style="margin-top:10px;display:none">
                                            <img id="previewImg" style="max-width:200px;max-height:200px">
                                        </div>
                                    </section>
                                    <section>
                                        <label class="label">内容</label>
                                        <label class="textarea">
                                            <i class="icon-append fa fa-file-text"></i>
                                            <textarea name="text" rows="5" class="form-control"></textarea>
                                        </label>
                                    </section>
                                    <section>
                                        <label class="label">附件</label>
                                        <div class="input-group">
                                            <label class="input">
                                                <i class="icon-append fa fa-file"></i>
                                                <input type="text" name="file" id="filePath" class="form-control" readonly>
                                            </label>
                                            <label for="attachFile" class="input-group-btn">
                                                <span class="btn btn-primary">
                                                    <i class="fa fa-folder-open"></i> 选择文件
                                                    <input type="file" name="attachFile" id="attachFile" style="display:none">
                                                </span>
                                            </label>
                                        </div>
                                    </section>
                                </fieldset>
                                <footer>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-save"></i> 保存
                                    </button>
                                    <button type="button" class="btn btn-default" onclick="window.history.back();">
                                        <i class="fa fa-reply"></i> 返回
                                    </button>
                                </footer>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="${resourcesUrl}/common/js/jquery-1.8.2.min.js"></script>
    <script src="${resourcesUrl}/common/js/bootstrap/bootstrap.min.js"></script>
    <script src="${resourcesUrl}/common/components/artDialog/dialog-min.js"></script>
    <script src="${resourcesUrl}/common/js/libs/ajaxfileupload.js"></script>
    <script type="text/javascript">
        $(function() {
            // 图片上传预览
            $('#picFile').change(function() {
                var file = this.files[0];
                if(file) {
                    if(!isImageFile(file)) {
                        dialog({
                            title: '提示',
                            content: '请选择图片文件（.jpg、.jpeg、.png、.gif）',
                            ok: function() {}
                        }).showModal();
                        $(this).val('');
                        return;
                    }
                    
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        $('#previewImg').attr('src', e.target.result);
                        $('#picPreview').show();
                    }
                    reader.readAsDataURL(file);
                    $('#picPath').val(file.name);
                }
            });

            // 文件上传
            $('#attachFile').change(function() {
                var file = this.files[0];
                if(file) {
                    if(!isAttachment(file)) {
                        dialog({
                            title: '提示',
                            content: '不支持的文件类型',
                            ok: function() {}
                        }).showModal();
                        $(this).val('');
                        return;
                    }
                    $('#filePath').val(file.name);
                }
            });

            // 表单提交
            $('#addForm').submit(function(e) {
                e.preventDefault();
                
                var formData = new FormData(this);
                
                $.ajax({
                    url: '${base}/content/save',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(data) {
                        if(data.RET_CODE.toLowerCase() === 'success') {
                            dialog({
                                title: '提示',
                                content: '保存成功',
                                ok: function() {
                                    window.location.href = '${base}/content/list?columnId=${columnId}';
                                }
                            }).showModal();
                        } else {
                            dialog({
                                title: '提示',
                                content: data.RET_OBJ || '保存失败',
                                ok: function() {}
                            }).showModal();
                        }
                    }
                });
            });
        });

        // 检查是否为图片文件
        function isImageFile(file) {
            var acceptedTypes = ['.jpg', '.jpeg', '.png', '.gif'];
            var fileName = file.name.toLowerCase();
            return acceptedTypes.some(function(type) {
                return fileName.endsWith(type);
            });
        }

        // 检查是否为允许的附件类型
        function isAttachment(file) {
            var acceptedTypes = ['.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.pdf', '.zip', '.rar'];
            var fileName = file.name.toLowerCase();
            return acceptedTypes.some(function(type) {
                return fileName.endsWith(type);
            });
        }
    </script>
    <%@ include file="/WEB-INF/page/common/footer.jsp"%>
</body>
</html> 